/**
 * MyProduct.java
 * 
 * Project: GWBS2009
 * 
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2007-2010 Shanghai Auto-Parts E-Procurement Co., Ltd.
 *        All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        APEP Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *
 */
package system.base;

import system.LoginContext;
import system.SelectManager;
import system.data.ProductBase;
import framework.core.Conditions;
import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBPreparedStatement;
import framework.core.DBResult;
import framework.core.DateUtil;
import framework.core.ILoginContext;
import framework.core.LogWriter;
import framework.core.ObjectList;
import framework.core.SearchEvent;
import framework.core.StringUtil;

public class MyProduct extends ProductBase {

	private String s_mhcx;

	//----system generated start----//
	public MyProduct()
	{

	}

	public static MyProduct fetch()
	{
		return new MyProduct();
	}
	public static MyProduct fetch(DBConnection con,String id) throws Exception
	{
		Conditions conditions = new Conditions();

		conditions.add(ID,id);

		return fetch(con,conditions);
	}

	public static MyProduct fetch(DBConnection con,Conditions conditions) throws Exception
	{
		DBResult result = null;
		try
		{
			conditions.add("T_PRODUCT.ID = T_MY_PRODUCT.PRODUCT_ID");
			result = DBEngine.doSelect(con,"select T_RPDOUCT.* from T_PRODUCT , T_MY_PRODUCT ",conditions);
			java.sql.ResultSet rs = result.getRs();
			MyProduct obj = null;
			if(rs.next())
			{
				obj = fetch(rs);
			}
			return obj;
		}
		finally
		{
			if(result!=null) {
				result.clear() ;
			}
		}
	}
	public static MyProduct fetch(java.sql.ResultSet rs) throws Exception
	{
		MyProduct obj = new MyProduct();
		obj.initObject(rs);
		return obj;
	}
	protected void getUnique(DBConnection con) throws Exception
	{
		setId(DBEngine.getId(con));
	}
	//----system generated end----//



	//----add by chengbiao.he start----//
	public String viewIsNormal(DBConnection con) throws Exception
	{
		return SelectManager.viewYesOrNo(getIsNormal()+"");
	}

	public String viewStatus(DBConnection con) throws Exception
	{
		return SelectManager.viewYesOrNo(getStatus()+"");
	}
	public String viewIsBuy(DBConnection con) throws Exception
	{
		return SelectManager.viewYesOrNo(getIsBuy()+"");
	}

	public String viewProviderCode(DBConnection con) throws Exception
	{
		Conditions cs = new Conditions();
		cs.add(Provider.CODE,getProviderCode());
		Provider pro = Provider.fetch(con, cs);
		if (pro == null) {
			return "";
		} else {
			return pro.viewName(con);
		}
	}

	//----add by chengbiao.he end----//
	//取得产品分类
	public  String viewCategoryCode(DBConnection con)throws Exception{
		Conditions conditions = new Conditions();
		conditions.add(WarehouseCategory.CATEGORY_CODE,getCategoryCode());
		WarehouseCategory wc = WarehouseCategory.fetch(con, WarehouseCategory.CATEGORY_CODE);
		wc.getCategoryName();
		return wc.getCategoryName();
	}

	//----add by wing start----//
	public String viewBalanceMode(DBConnection con) throws Exception
	{
		return SelectManager.viewBalanceModel(getBalanceMode()+"");
	}

	//----add by wing end----//

	// add by Jackey start --//
	public String viewWarehouseName(DBConnection con) throws Exception
	{
		String mReturn = "";
		Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());
		if (w != null)
		{
			mReturn = w.getName();
		}
		return  mReturn;
	}

	public String viewIsTradeflatProduct(DBConnection con) throws Exception
	{
		String mReturn = "";
		if (getIsTradeflatProduct().equalsIgnoreCase(ConstDefine.STR_VAL_DEAL))
		{
			mReturn = ConstDefine.STR_NAME_DEAL;
		}
		else
		{
			mReturn = ConstDefine.STR_NAME_HAND;
		}
		return mReturn;
	}

	public String viewUnitPrice(DBConnection con) throws Exception
	{
		return StringUtil.showPrice4(String.valueOf(getUnitPrice()));
	}


	public ObjectList search(DBConnection con,SearchEvent event) throws Exception
	{
		DBEngine.checkCon(con);
		String sql = "select distinct T_PRODUCT.* from T_PRODUCT , T_MY_PRODUCT ";
		String sql1 = "select count(distinct T_PRODUCT.ID) from T_PRODUCT , T_MY_PRODUCT ";
		Conditions eventCs = event.toConditions();
		eventCs.add("T_PRODUCT.ID = T_MY_PRODUCT.PRODUCT_ID");
		int rsCount = DBEngine.doCount(con, sql1, eventCs);
		Conditions cs = event.toConditions();
		cs.add("T_PRODUCT.ID = T_MY_PRODUCT.PRODUCT_ID");
		DBResult result = DBEngine.doSelect(con,sql,cs,event);
		ObjectList list = ObjectList.fromRsMini(getClass(),result.getRs(),event.getPageSize(),event.getPageNum(), rsCount);
		result.clear();
		return list;
	}

	public ObjectList search(DBConnection con,Conditions conditions,String sort) throws Exception
	{
		DBEngine.checkCon(con);
		String sql = "select distinct T_PRODUCT.* from T_PRODUCT , T_MY_PRODUCT ";
		conditions.add("T_PRODUCT.ID = T_MY_PRODUCT.PRODUCT_ID");
		DBResult result = DBEngine.doSelect(con,sql,conditions,sort);
		ObjectList list = ObjectList.fromRs(getClass(),result.getRs());
		result.clear();
		return list;
	}

	public static Product fetchByConcentricityCodeAndAPEPCode(DBConnection con, String concentricityCode, String apepCode) throws Exception {
		Conditions cs = new Conditions();
		cs.add(Product.APEP_CODE, apepCode);
		cs.add(Product.CONCENTRICITY_CODE, concentricityCode);
		return Product.fetch(con, cs);
	}



	private static final String SQL_EXIST_MY_PRODUCT = "select count(*) from t_my_product ";
	private static final String SQL_INSERT_MY_PRODUCT = "insert into t_my_product (ID, WAREHOUSE_CODE, PRODUCT_ID, CREATE_TIME, CREATE_USER) values (S_MY_PRODUCT.nextval,?,?,?,?)";
	public static void insertMyProduct(LoginContext loginContext, String warehouseCode, String concentricityCode, String apepCode) {
		DBConnection con = null;
		DBPreparedStatement prp = null;

		try {
			con = DBConnection.open();
			Product tProduct = fetchByConcentricityCodeAndAPEPCode(con, concentricityCode, apepCode);

			if(tProduct == null) {
				return;
			}

			Conditions cs = new Conditions();
			cs.add("PRODUCT_ID", tProduct.getId());
			cs.add("WAREHOUSE_CODE", warehouseCode);
			if(DBEngine.doCount(con, SQL_EXIST_MY_PRODUCT, cs) > 0) {
				return;
			}

			prp = con.prepareStatement(SQL_INSERT_MY_PRODUCT);
			int i = 1;
			prp.setString(i++, warehouseCode);
			prp.setString(i++, tProduct.getId());
			prp.setString(i++, new DateUtil().getFullString());
			prp.setString(i++, loginContext.getLoginUserName());
			prp.executeUpdate();

		} catch(Exception e) {
			LogWriter.error(e.getLocalizedMessage(), e);
		} finally {
			try {
				if(prp != null) {
					prp.close();
				}

				if(con != null) {
					con.close();
				}
			} catch(Exception e) {
				LogWriter.error(e.getLocalizedMessage(), e);
			}
		}
	}


	public static void insertMyProduct(DBConnection con, ILoginContext loginContext, String warehouseCode, String id) {
		DBPreparedStatement prp = null;

		try {
			Product tProduct = Product.fetch(con, id);

			if(tProduct == null) {
				return;
			}

			Conditions cs = new Conditions();
			cs.add("PRODUCT_ID", tProduct.getId());
			cs.add("WAREHOUSE_CODE", warehouseCode);
			if(DBEngine.doCount(con, SQL_EXIST_MY_PRODUCT, cs) > 0) {
				return;
			}

			prp = con.prepareStatement(SQL_INSERT_MY_PRODUCT);
			int i = 1;
			prp.setString(i++, warehouseCode);
			prp.setString(i++, tProduct.getId());
			prp.setString(i++, new DateUtil().getFullString());
			prp.setString(i++, loginContext.getLoginUserName());
			prp.executeUpdate();

		} catch(Exception e) {
			LogWriter.error(e.getLocalizedMessage(), e);
		} finally {
			try {
				if(prp != null) {
					prp.close();
				}

			} catch(Exception e) {
				LogWriter.error(e.getLocalizedMessage(), e);
			}
		}


	}

}
